package com.redoor.app.stake.basicdata.service.impl;

import com.redoor.app.stake.basicdata.model.BaseRegistration;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseRegistrationRedisService;
import com.redoor.app.stake.common.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by wangjunlei on 2018-03-10 17:05:59.
 */
@Service
public class BaseRegistrationRedisServiceImpl implements BaseRegistrationRedisService {
    private Logger logger = LoggerFactory.getLogger(BaseRegistrationRedisServiceImpl.class);
    @Autowired
    RedisService redisService;

    @Override
    public boolean modifyInfo(BaseRegistration bean) {
        if (bean==null || StringUtils.isEmpty(bean.getUuid())){
            logger.error("uuid is empty");
            return false;
        }
        try {
            String key = BasicDataRedisConstants.REDIS_REGISTRATION_PRE + bean.getUuid();
            redisService.add(key,bean,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);
            redisService.add(BasicDataRedisConstants.REDIS_REGISTRATION_PRE + bean.getNumber() + ":UUID",bean.getUuid(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 12);
        }catch (Exception e){
            logger.error(e.getLocalizedMessage());
            return false;
        }
        return true;
    }

    @Override
    public BaseRegistration getRegistration(String uuid) {
        return (BaseRegistration) redisService.get( BasicDataRedisConstants.REDIS_REGISTRATION_PRE + uuid);
    }

    @Override
    public String getUuidByNumber(String number) {
        return (String) redisService.get(BasicDataRedisConstants.REDIS_REGISTRATION_PRE + number + ":UUID");
    }
}